package com.situ.day4;

import java.util.Scanner;

import org.junit.Test;

public class Homework {

	@Test
	public void test1() {
		int[] scores = { 0, 0, 1, 2, 3, 5, 4, 5, 2, 8, 7, 6, 9, 5, 4, 8, 3, 1, 0, 2, 4, 8, 7, 9, 5, 2, 1, 2, 3, 9 };
		int count = 0;
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j < scores.length; j++) {
				if (scores[j] == i) {
					count++;
				}
			}
			System.out.println(count);
			count = 0;
		}
	}

	@Test
	public void test12() {
		int[] scores = { 0, 0, 1, 2, 3, 5, 4, 5, 2, 8, 7, 6, 9, 5, 4, 8, 3, 1, 0, 2, 4, 8, 7, 9, 5, 2, 1, 2, 3, 9 };
		int[] count = new int[10];
		for (int i = 0; i < scores.length; i++) {
			switch (scores[i]) {
			case 0:
				count[0]++;
				break;
			case 1:
				count[1]++;
				break;
			case 2:
				count[2]++;
				break;
			case 3:
				count[3]++;
				break;
			case 4:
				count[4]++;
				break;
			case 5:
				count[5]++;
				break;
			case 6:
				count[6]++;
				break;
			case 7:
				count[7]++;
				break;
			case 8:
				count[8]++;
				break;
			case 9:
				count[9]++;
				break;
			default:
				break;
			}
		}
		for (int i = 0; i < count.length; i++) {
			System.out.println(count[i]);
		}
	}

//	要求求出其中的奇数个数和偶数个数。
	@Test
	public void test2() {
		int[] scores = { 0, 0, 1, 2, 3, 5, 4, 5, 2, 8, 7, 6, 9, 5, 4, 8, 3, 1, 0, 2, 4, 8, 7, 9, 5, 2, 1, 2, 3, 9 };
		int oddCount = 0; // 奇数个数
		int evenCount = 0; // 偶数个数
		for (int i = 0; i < scores.length; i++) {
			if (scores[i] % 2 == 0) {
				evenCount++;
			} else {
				oddCount++;
			}
		}
		System.out.println(oddCount);
		System.out.println(evenCount);
	}

//	输入一组学生的成绩，使用数组，然后计算他们的平均值.
	@Test
	public void test3() {
		Scanner scanner = new Scanner(System.in);
		int average = 0;
		int sum = 0;
		System.out.println("请输入学生成绩：");
		int[] scores = new int[10];
		for (int i = 0; i < scores.length; i++) {
			scores[i] = scanner.nextInt();
			sum = sum + scores[i];
		}
		average = sum / 10;
		System.out.println(average);
	}

//	题目：一个5位数，判断它是不是回文数。即12321是回文数，个位与万位相同，十位与千位相同。
	@Test
	public void test4() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个字符串：");
		String str = scanner.next();
		char[] array = str.toCharArray();
		// 初始默认是回文数
		boolean isPalindrome = true;
		for (int i = 0; i < array.length; i++) {
			if (array[i] == array[array.length - 1 - i]) {
				isPalindrome = true;
			} else {
				isPalindrome = false;
				break;
			}
		}
		if (isPalindrome == true) {
			System.out.println(str + "是回文数");
		} else {
			System.out.println(str + "不是回文数");
		}
	}
	
//	题目：一个5位数，判断它是不是回文数。即12321是回文数，个位与万位相同，十位与千位相同。 
//	第二种方法
	@Test
	public void test8() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个字符串：");
		String str = scanner.next();
		char[] array = str.toCharArray();
		// 初始默认是回文数
		boolean isPalindrome = true;
		for (int i = 0; i < array.length / 2; i++) {
			if (array[i] != array[array.length - 1 - i]) {
				isPalindrome = false;
				break;
			}
		}
		if (isPalindrome == true) {
			System.out.println(str + "是回文数");
		} else {
			System.out.println(str + "不是回文数");
		}
	}

//	17、输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。
	@Test
	public void test5() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一行字符：");
		String str = scanner.nextLine();
		int letterCount = 0;
		int spaceCount = 0;
		int digitCount = 0;
		int otherCount = 0;
		char[] array = str.toCharArray();
		for (int i = 0; i < array.length; i++) {
			if ((array[i] >= 'A' && array[i] <= 'Z') 
			 || (array[i] >= 'a' && array[i] <= 'z')) {
				letterCount++;
			} else if (array[i] == ' ') {
				spaceCount++;
			} else if (array[i] >= '0' && array[i] <= '9') {
				digitCount++;
			} else {
				otherCount++;
			}
		}
		System.out.println("英文字母" + letterCount + "个");
		System.out.println("空格" + spaceCount + "个");
		System.out.println("数字" + digitCount + "个");
		System.out.println("其他字符" + otherCount + "个");
	}

//	18、int[] = {23,4,56,78,99,22,67}找出数组里面的最大值和最小值
	@Test
	public void test6() { // 利用冒泡排序法解决
		int[] array = { 23, 4, 56, 78, 99, 22, 67 };
		for (int i = 0; i < array.length - 1; i++) {
			for (int j = 0; j < array.length - 1 - i; j++) {
				if (array[j] > array[j + 1]) {
					int temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
				}
			}
		}
		System.out.println("数组最小值为" + array[0]);
		System.out.println("数组最大值为" + array[array.length - 1]);
	}

	// 18题第二种方式
	@Test
	public void test7() {
		int[] array = { 23, 4, 56, 78, 99, 22, 67 };
		// 找出最大值
		int max = array[0];
		// 找出最小值
		int min = array[0];
		for (int i = 0; i < array.length - 1; i++) {
			if (max < array[i + 1]) {
				max = array[i + 1];
			}
			if (min > array[i + 1]) {
				min = array[i + 1];
			}
		}
		System.out.println("数组最大值为" + max);
		System.out.println("数组最小值为" + min);
	}
	
}
